﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class ZZZZElement:NmonStatElement
    {
        protected new ILog _log = LogManager.GetLogger(typeof(ZZZZElement));
        protected IDataParameter _value = null;
        protected IDataParameter _zzzz = null;
        public string Zzzz { get; protected set; }
        public DateTime Value { get; protected set; }
        public DateTime DatetimeFile { get;  set; }
        public ZZZZElement(IDbConnection connection)
        {
            Balise = BaliseType.CPU;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["ZZZZ"] + " ([zzzz] ,[value]) VALUES (@zzzz,@value)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _value = Command.CreateParameter();
            _value.ParameterName = "@value";
            _value.DbType = DbType.DateTime;
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            Value = DateTime.MinValue;
        }
        public ZZZZElement():this(null){}
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _value.Value = Value;
                _zzzz.Value = Zzzz;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_value);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("ZZZZ"))
                {
                    _log.Error("Not ZZZZ Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                
                Zzzz = elements[1];
                if (Value==DateTime.MinValue)
                {
                    Value = DatetimeFile;
                }
                DateTime res;
                if (elements.Length > 3)
                {
                    if (DateTime.TryParse(elements[3] + " " + elements[2], CultureInfo.InvariantCulture, DateTimeStyles.None, out res))
                    {
                        if (res < Value)
                        {
                            Value = res.AddDays(1);
                        }
                        else
                        {
                            Value = res;
                        }
                        return true;
                    }
                    else
                    {
                        if (DateTime.TryParseExact(elements[3] + " " + elements[2],"dd-MMM-yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out res))
                        {
                            if (res < Value)
                            {
                                Value = res.AddDays(1);
                            }
                            else
                            {
                                Value = res;
                            }
                            return true;
                        }
                    }
                }
                else
                {
                    if (DateTime.TryParseExact(Value.Date.ToString("MM/dd/yyyy") + " " + elements[2], "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out res))
                    {
                        if (res < Value)
                        {
                            Value = res.AddDays(1);
                        }
                        else
                        {
                            Value = res;
                        }
                        return true;
                    }
                }
                


                return false;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
